package MediaOnline.GUI;

import java.io.File;
import java.net.URL;

import MediaOnline.Adapter.DownloadedFilmAdapter;
import MediaOnline.Adapter.VideoAdapter;
import MediaOnline.Data.*;
import MediaOnline.Adapter.VideoAdapter2;
import MediaOnline.Data.ListFilmForMedia;
import MediaOnline.Data.filmDetail;
import MediaOnline.Data.videoData;
import MediaOnline.General.checkNetwork;
import MediaOnline.General.paramConst;
import MediaOnline.WebService.GetFilmByID;
import MediaOnline.WebService.HTTPTest;
import MediaOnline.WebService.SessionRegister;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class actListFilm extends Activity {
	static VideoAdapter adpListVideo;
	static VideoAdapter2 adpListVideo2;
	static GridView gVideoList1;
	public static String status="status";
	public static int FilmCatID=-1;
	public static Context ctx;
	public static int width=0;
	static boolean ChiTiet=true;
	String FileFilmPath="";
	String FileAnhPath="";
	String FileName="";
	private static final int DELETE_MENU_ITEM = Menu.FIRST;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filmmoi);
		FilmCatID=-1;
		gVideoList1 = (GridView) findViewById(R.id.gVideoList1);
		ctx = actListFilm.this;
		//Tao cac adapter
		adpListVideo  = new VideoAdapter(getApplicationContext());
		adpListVideo2  = new VideoAdapter2(getApplicationContext());
		//Load list movie tren server 
		adpListVideo.getListVideo(actListFilm.this,FilmCatID);
		//Kiem tra do phan giai man hinh
		Display display = getWindowManager().getDefaultDisplay();
		width = (int) (display.getWidth());
		int Cot = width/400;
		gVideoList1.setNumColumns(1);
		
		gVideoList1.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> a, View v, int i,
					long l) {
				// TODO Auto-generated method stub
				ListFilmForMedia.Tap=0;
				//Nếu là danh sách online
				if (!actFilmMain3.getDownloaded) {
					//index = i;
					filmDetail.FilmID=videoData.lstVideoID.get(i);
					filmDetail.Url = videoData.lstVideoLink.get(i);
					//Them vao list film for media (Tap 1)
					ListFilmForMedia.lstFilmUrl.clear();
					ListFilmForMedia.lstFilmID.clear();
					ListFilmForMedia.lstTap.clear();
					ListFilmForMedia.lstFilmID.add(videoData.lstVideoID.get(i));
					ListFilmForMedia.lstFilmUrl.add(videoData.lstVideoLink.get(i));
					ListFilmForMedia.lstTap.add(1);
					//Start activity
					startActivity(new Intent(actListFilm.this, actFilmDetailInfo2.class));
					GetFilmByID g = new GetFilmByID(actListFilm.this);
					g.get();	
				}
				//Nếu là danh sách đã download
				else {
					FileFilmPath = DownloadedFilmAdapter.arrListFilmPath.get(i);
					FileName = DownloadedFilmAdapter.arrListFilmName.get(i);
					FileAnhPath = paramConst.DownloadedFolder+DownloadedFilmAdapter.arrListFilmName.get(i)+".jpg";
					Log.d("File ảnh path::::::", FileAnhPath);
					Toast.makeText(ctx, "Film path = " + DownloadedFilmAdapter.arrListFilmPath.get(i), Toast.LENGTH_SHORT).show();
				}
				
				//Toast.makeText(actListFilm.this, videoData.lstVideoID.get(i) , Toast.LENGTH_SHORT).show();
			}
		});
		
		//Xu li su kien longclick
		gVideoList1.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View v,
					int i, long arg3) {
				// TODO Auto-generated method stub
				Log.d("actFilmMain3.getDownloaded::::::", ""+actFilmMain3.getDownloaded);
				//Nếu là danh sách online
				if (!actFilmMain3.getDownloaded){
					//Nếu là phim bộ thì phải vào phim infor
					if (videoData.lstSoTap.get(i)>1) {
						filmDetail.FilmID=videoData.lstVideoID.get(i);
						filmDetail.Url = videoData.lstVideoLink.get(i);
						//Them vao list film for media (Tap 1)
						ListFilmForMedia.lstFilmID.add(videoData.lstVideoID.get(i));
						ListFilmForMedia.lstFilmUrl.add(videoData.lstVideoLink.get(i));
						ListFilmForMedia.lstTap.add(1);
						//Start activity
						startActivity(new Intent(actListFilm.this, actFilmDetailInfo2.class));
						GetFilmByID g = new GetFilmByID(actListFilm.this);
						g.get();	
					}
					//Nếu là phim lẻ thì play
					else {
						checkNetwork chk  = new checkNetwork();
						/*if (chk.UseMobileConnection(ctx)) {
							//============Hien thi thong bao=======================
							AlertDialog alertDialog = new AlertDialog.Builder(actListFilm.this).create();
							alertDialog.setTitle("Thông báo!");
							alertDialog.setMessage("Để đảm bảo xem phim với chất lượng tốt nhất. Chúng tôi chỉ đáp ứng yêu cầu xem phim khi sử dụng mạng có dây hoặc wifi. Mong quý khách thông cảm!");
							alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
							   public void onClick(DialogInterface dialog, int which) {

							   }
							});
							alertDialog.show();
							//============================================================================
						}
						else{*/
							ListFilmForMedia.lstFilmUrl.clear();
							ListFilmForMedia.lstFilmID.clear();
							ListFilmForMedia.lstTap.clear();
							filmDetail.FilmNameChinh = videoData.lstVideo.get(i);

							SessionRegister s = new SessionRegister(ctx, videoData.lstVideoLink.get(i));
							s.reg();
						//}
					}
					
					//Kiểm tra nếu dùng các kết nối chậm thì không cho xem phim
					
				}
				//Nếu là danh sách offline
				else {
					FileFilmPath = DownloadedFilmAdapter.arrListFilmPath.get(i);
					FileAnhPath = DownloadedFilmAdapter.arrListFilmName.get(i)+".jpg";
					FileName = DownloadedFilmAdapter.arrListFilmName.get(i);
					registerForContextMenu(v);
					
					
					/*Intent intent = new Intent(actListFilm.this, Nahi_MediaActivity.class);
					Bundle newBundle = new Bundle();
					newBundle.putString("Link", DownloadedFilmAdapter.arrListFilmPath.get(i));
					intent.putExtras(newBundle);
					startActivity(intent);
					Toast.makeText(ctx, "Longclick -- Film path = " + DownloadedFilmAdapter.arrListFilmPath.get(i), Toast.LENGTH_SHORT).show();*/
				}
				
				return false;
			}
		});
		
	}
	
	public static void ViewList(){
			if(ChiTiet==false){
				
				int Cot = width/250;
				gVideoList1.setNumColumns(Cot);
				gVideoList1.setAdapter(adpListVideo);
			}
			if(ChiTiet==true){
				gVideoList1.setNumColumns(1);
				gVideoList1.setAdapter(adpListVideo2);
			}
	}
	
	//Xu ly khi chon the loai phim
	public static void LoadFilmByCat(int FilmCatID){
		adpListVideo  = new VideoAdapter(ctx);
		//Load list movie tren server 
		adpListVideo.getListVideo(ctx,FilmCatID);
	}
	
	//Xu ly button + -
	public static void UpdateList(int i){
		//+
		if(i==1){
			if(ChiTiet!=true){
				gVideoList1.setAdapter(adpListVideo2);
				gVideoList1.setNumColumns(1);
				ChiTiet=true;
			}
		}
		//-
		else{
			if(ChiTiet){
				int Cot = width/250;
				gVideoList1.setNumColumns(Cot);
				gVideoList1.setAdapter(adpListVideo);
				ChiTiet=false;
			}
		}
	}
	
	//Update film moi sau khi chon the loai phim
	public static void UpdateListFilm(){
		gVideoList1.setAdapter(adpListVideo2);
	}
	
	//Update list film downloaded
	
	public static void UpdateListDownloadedFilm(){
		int Cot = width/250;
		gVideoList1.setNumColumns(Cot);
		DownloadedFilmAdapter adpDownload = new DownloadedFilmAdapter(ctx);
		adpDownload.getListDownloadedFilm();
		gVideoList1.setAdapter(adpDownload);
	}
	
	//Menu 
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_MENU_ITEM:
			//Delete file film
			File file = new File(FileFilmPath);
			boolean deleted = file.delete();
			//Delete file ảnh
			File fileanh = new File(FileAnhPath);
			boolean deletedanh = fileanh.delete();
			if (deleted && deletedanh) {
				showMsg("Đã xóa "+FileName);
				UpdateListDownloadedFilm();
			}
			else {
				showMsg("Xóa tập tin thất bại!");
			}
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.setHeaderTitle("Menu...");
		menu.add(0, DELETE_MENU_ITEM, 0, "Xóa");
		

	}
	
	private void showMsg(String message) {
		Toast msg = Toast.makeText(ctx, message, Toast.LENGTH_LONG);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
				msg.getYOffset() / 2);
		msg.show();
	}
	
	public static void PlayFilm(){
		Intent intent = new Intent(ctx, Nahi_MediaActivity.class);
		Bundle newBundle = new Bundle();
		newBundle.putString("Link",paramConst.URL_XEM_PHIM+"?u="+userData.Username);
		Log.d("session Bundle link:::	", paramConst.URL_XEM_PHIM+"?u="+userData.Username);
		intent.putExtras(newBundle);
		ctx.startActivity(intent);
		
		
		
	}
	
	
}
